home *** CD-ROM | disk | FTP | other *** search
- Path: news.austin.sar.slb.com!usenet
- From: Chad Houston <chouston@slb.com>
- Newsgroups: comp.lang.c++
- Subject: [Q] String class problem
- Date: Thu, 04 Apr 1996 12:34:54 -0600
- Organization: Schlumberger GeoQuest
- Message-ID: <3164164E.6E6F@slb.com>
- NNTP-Posting-Host: talkingheads.austin.sar.slb.com
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.01 (X11; I; SunOS 5.4 sun4m)
-
- I've recently hit a compiler snag on the SGI with a string class that we've
- been using for a while. It compiles fine on our Solaris 2.4 compilers, but
- not on IRIX 5.3. Regardless of the platform, maybe someone can recognize an
- error in my code.
-
- Any help would be greatly appreciated.
-
- Here's a stripped down version of the class:
-
- util_string.hh
- 1
- 2 class util_String
- 3 {
- 4 public:
- 5 util_String(const char*);
- 6 util_String(const util_String&);
- 7 util_String(int);
- 8 util_String(float);
- 9 util_String(double);
- 10 util_String();
- 11 ~util_String() { delete Str; }
- 12
- 13 util_String& operator=(const util_String&);
- 14 util_String& operator+=(const util_String&);
- 15 operator char*();
- 16
- 17 private:
- 18 int Length;
- 19 char *Str;
- 20 };
-
-
- Here is a short test program which illustrates the compiler problem:
-
- string_test.cc
- 1
- 2 #include <util_string.hh>
- 3
- 4 int
- 5 main()
- 6 {
- 7 int iVal = 5;
- 8 float fVal = 5.12345;
- 9 char cpSpace[] = " ";
- 10 char cpVal[] = "Test String";
- 11 util_String MyString;
- 12
- 13 MyString = cpVal;
- 14 MyString += cpSpace;
- 15 MyString += iVal;
- 16 // MyString += (util_String) iVal;
- 17 // MyString.cps_String::operator+=(iVal);
- 18 MyString += cpSpace;
- 19 MyString += fVal;
- 20 }
-
-
- The SGI /bin/CC compiler returns the following error:
-
- test(200)% !CC
- CC -c -I. string_test.cc
- "string_test.cc", line 18: error(3431): more than one operator "+=" matches
- these operands:
- built-in operator "pointer += integer"
- function "cps_String::operator+="
- MyString += iVal;
-
-
- Apparently, it can't determine that it should use the util_String(int) constructor,
- and then call the util_String::operator+=(const util_String&) method. However, the
- file *will* compile if I comment out line 15 in the header file. That is, if
- I remove my method to cast a util_String to a char *. Is there something wrong
- with the syntax of my cast operator, operator char *() ?
-
- Note that the file will also compile if I comment out line 15 in string_test.cc
- and use either line 16 or 17.
-
- --
- +--------------+
- |______________| Chad Houston <chouston@slb.com>
- | Schlumberger | Senior Software Engineer CPS-3 Engineering
- +--------------+ Austin, TX, USA
- GeoQuest Phone: 512.331.3384 Fax: 512.331.3387
-